﻿using Devonline.AspNetCore;
using Devonline.Identity.Admin.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Query;

namespace Devonline.Identity.Admin.Controllers;

/// <summary>
/// 基础数据管理
/// </summary>    
[Route("api/[controller]")]
[SecurityHeaders]
[ApiController]
[Authorize(Roles = AppSettings.GROUP_MAINTAINERS)]
public class ParametersController : ControllerBase
{
    private readonly string _userName;
    private readonly ILogger<ParametersController> _logger;
    private readonly IDataService<IdentityDbContext, Parameter> _dataService;

    public ParametersController(ILogger<ParametersController> logger, IDataService<IdentityDbContext, Parameter> dataService)
    {
        _userName = User.Identity.Name;
        _logger = logger;
        _dataService = dataService;
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [EnableQuery]
    public IActionResult Get()
    {
        _logger.LogInformation("user {user} query the parameter list", _userName);
        return Ok(_dataService.GetQueryable());
    }

    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="viewModel"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<IActionResult> CreateAsync(ParameterViewModel viewModel)
    {
        _logger.LogInformation("user {user} will add the parameter {parameter}", _userName, viewModel.Key);

        var parameter = await _dataService.AddAsync(viewModel);
        var result = await _dataService.SaveChangesAsync();
        if (result >= 1)
        {
            _logger.LogWarning("user {user} success to add the parameter {parameter}", _userName, parameter.Key);
            return Ok();
        }

        return BadRequest($"创建基础数据 {viewModel.Key} 没有成功!");
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="viewModel"></param>
    /// <returns></returns>
    [HttpPut("{id}")]
    public async Task<IActionResult> ChangeAsync(string id, ParameterViewModel viewModel)
    {
        _logger.LogInformation("user {user} will change the parameter {parameter}", _userName, id);

        var parameter = await _dataService.AddAsync(viewModel);
        var result = await _dataService.SaveChangesAsync();
        if (result >= 1)
        {
            _logger.LogWarning("user {user} success to change the parameter {parameter}", _userName, parameter.Key);
            return Ok();
        }

        return BadRequest($"修改基础数据 {viewModel.Key} 没有成功!");
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpDelete("{id}")]
    public async Task<IActionResult> DeleteAsync(string id)
    {
        await _dataService.DeleteAsync(id);
        var result = await _dataService.SaveChangesAsync();
        if (result >= 1)
        {
            _logger.LogWarning("user {user} success to delete the parameter {parameter}", _userName, id);
            return Ok();
        }

        return BadRequest($"删除基础数据 {id} 出错!");
    }
}
